package com.wHitE.dao.impl;

import com.wHitE.dao.LoginDao;

import java.sql.*;


public class LoginDaoImpl implements LoginDao {
    private String password;

    @Override
    public String userLogin(String username) {
        try {
            // 连接（JDBC）数据库并将查询数据从数据库（MySQL）中
            // 1.加载数据库驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2.连接数据库
            Connection connection = DriverManager.getConnection("jdbc:mysql:" +
                    "//localhost:3306/javaweb_database", "root", "123456");
            // 3.发送SQL语句
            String sql = "select * from user where username=?";
            // 预处理
            PreparedStatement pst = connection.prepareStatement(sql);
            pst.setString(1, username);
            // 执行
            ResultSet resultSet = pst.executeQuery();
            password = null;
            if (resultSet.next()) {
                password = resultSet.getString("password");
            }
            // 关闭执行
            pst.close();
            // 断开连接
            connection.close();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return password;
    }
}
